package com.jlastu.znb.view;

import static android.app.Activity.RESULT_OK;
import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jlastu.znb.R;
import com.jlastu.znb.base.BaseFragment;
import com.jlastu.znb.contract.MineContract;
import com.jlastu.znb.entity.User;
import com.jlastu.znb.presenter.MinePresenter;
import com.jlastu.znb.utils.SpUtils;

import java.util.Random;

public class MineFragment extends BaseFragment implements View.OnClickListener, MineContract.IMineView {

    private TextView mTvName;
    private TextView mName;
    private TextView mTvNumber;
    private ImageView mImageView;
    private ImageView mUserPicture;
    private TextView mTextView;
    private LinearLayout mLinearLayout1;
    private LinearLayout mLinearLayout2;
    private LinearLayout mLinearLayout3;
    private TextView mOrdersTeext;
    private ImageView mOrdersImage;
    private LinearLayout mOrdersLinearLayout1;
    private LinearLayout mOrdersLinearLayout2;
    private LinearLayout mOrdersLinearLayout3;
    private LinearLayout mOrdersLinearLayout4;
    private LinearLayout mOrdersLinearLayout5;
    private LinearLayout mOrdersLinearLayout6;
    private RelativeLayout mSettingLayout;
    private MineContract.IMinePresenter mMinePresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        mMinePresenter = new MinePresenter(this, getContext());
        mSettingLayout = mView.findViewById(R.id.settingView);
        mName = mView.findViewById(R.id.name);
        mTvName = mView.findViewById(R.id.user_name);
        mTvNumber = mView.findViewById(R.id.mine_number);

        SharedPreferences prefs = getActivity().getSharedPreferences("UserPrefs", MODE_PRIVATE);
        String registeredUsername = prefs.getString("Username", "");
        String phone = prefs.getString("phone", "");

        mTvNumber.setText("电话号码："+phone);

        mTvName.setText("用户名：" + registeredUsername);
        mUserPicture = mView.findViewById(R.id.user_picture);
        mImageView = mView.findViewById(R.id.go_set_image);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SetActivity.class);
                startActivity(intent);
            }
        });
        mTextView = mView.findViewById(R.id.go_set_text);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SetActivity.class);
                startActivity(intent);
            }
        });
        mLinearLayout1 = mView.findViewById(R.id.collect);
        mLinearLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CollectActivity.class);
                startActivity(intent);
            }
        });
        mLinearLayout2 = mView.findViewById(R.id.shopping_cart);
        mLinearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), ShoppingCartActivity.class);
                startActivity(intent);
            }
        });
        mLinearLayout3 = mView.findViewById(R.id.footprint);
        mLinearLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FootprintActivity.class);
                startActivity(intent);
            }
        });
        mOrdersTeext = mView.findViewById(R.id.mine_orders_all);
        mOrdersTeext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                startActivity(intent);
            }
        });
        mOrdersImage = mView.findViewById(R.id.mine_orders_all_image);
        mOrdersImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                startActivity(intent);
            }
        });
        mOrdersLinearLayout1 = mView.findViewById(R.id.linearlayout1);
        mOrdersLinearLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("SELECT_DEFAULT_TITLE", "orders_title2"); // 添加标志
                startActivity(intent);
            }
        });
        mOrdersLinearLayout2 = mView.findViewById(R.id.linearlayout2);
        mOrdersLinearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("SELECT_DEFAULT_TITLE", "orders_title3"); // 添加标志
                startActivity(intent);
            }
        });
        mOrdersLinearLayout3 = mView.findViewById(R.id.linearlayout3);
        mOrdersLinearLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("SELECT_DEFAULT_TITLE", "orders_title4"); // 添加标志
                startActivity(intent);
            }
        });
        mOrdersLinearLayout4 = mView.findViewById(R.id.linearlayout4);
        mOrdersLinearLayout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("SELECT_DEFAULT_TITLE", "orders_title5"); // 添加标志
                startActivity(intent);
            }
        });
        mOrdersLinearLayout5 = mView.findViewById(R.id.linearlayout5);
        mOrdersLinearLayout5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("SELECT_DEFAULT_TITLE", "orders_title6"); // 添加标志
                startActivity(intent);
            }
        });

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        String photoPath = sharedPreferences.getString("PHOTO_PATH_KEY", null);
        if (photoPath != null) {
            Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
            mUserPicture.setImageBitmap(bitmap);
        }

        String savedName = sharedPreferences.getString("NAME_KEY", "");
        // 假设name文本视图原本显示"姓名："
        mName.setText("姓名：" + savedName);

    }



    @Override
    protected void initListener() {
        //mSettingLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.settingView) {

        }
    }


    @Override
    public void showUserInfo(User user) {

    }
}
